//
//  HLVideoPostViewController.m
//  YueYue
//
//  Created by hulu001 on 2024/3/8.
//

#import "HLVideoPostViewController.h"
#import "HLVideoPostMediaView.h"
#import "HLOSSManager.h"
#import "HLVideoOptionRequest.h"

@interface HLVideoPostViewController ()<QMUITextViewDelegate>

@property (nonatomic, strong) QMUIButton *saveButton;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) HLVideoPostMediaView *mediaView;
@property (nonatomic, strong) QMUITextView *textView;
@property (nonatomic, strong) UILabel *textCountLabel;
@property (nonatomic, strong) HLVideoPostModel *squarePost;
@property (nonatomic, strong) HLOSSManager *squareImageOSSManager;

@end

@implementation HLVideoPostViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.squareImageOSSManager = [[HLOSSManager alloc] initWithOSSPath:@"dynamic-pic"];
    [self.mediaView updateMediaTypeWithPostType:1];
}


- (void)setupNavigationItems
{
    [super setupNavigationItems];
    
    self.title = @"发布";
    
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithCustomView:({
        QMUIButton *saveButton = [[QMUIButton alloc] init];
        [saveButton setTitle:@"发布" forState:UIControlStateNormal];
        [saveButton.titleLabel setFont:UIFontMediumMake(12)];
        [saveButton setBackgroundColor:HLMainColor];
        [saveButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
        [saveButton setContentEdgeInsets:UIEdgeInsetsMake(6, 12, 6, 12)];
        saveButton.cornerRadius = 14;
        [saveButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
        self.saveButton = saveButton;
        saveButton;
    })];
    
    self.navigationItem.rightBarButtonItem = saveItem;
}

- (void)initSubviews
{
    [super initSubviews];
    
    self.squarePost = [[HLVideoPostModel alloc] init];
    
    self.scrollView = [[UIScrollView alloc] init];
    [self.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.contentView = [[UIView alloc] init];
    _contentView.backgroundColor = UIColorWhite;
    [self.scrollView addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    self.mediaView = [[HLVideoPostMediaView alloc] initWithPost:self.squarePost];
    [_contentView addSubview:_mediaView];
    [_mediaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(NavigationContentTop + 10);
    }];
    
    self.textView = [[QMUITextView alloc] init];
    _textView.placeholder = @"分享您的生活，发布做菜相关的视频、";
    _textView.font = UIFontMediumMake(14);
    _textView.textColor = HLTextColor1;
    //_textView.contentInset = UIEdgeInsetsMake(16, 20, 16, 20);
    _textView.backgroundColor = UIColorWhite;
    _textView.maximumTextLength = 300;
    _textView.delegate = self;
    [self.contentView addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(20);
        make.top.equalTo(_mediaView.mas_bottom).offset(2);
        make.height.mas_equalTo(112);
    }];
    
    self.textCountLabel = [[UILabel alloc] init];
    _textCountLabel.font = UIFontMake(14);
    _textCountLabel.textColor = HLTextColor3;
    _textCountLabel.text = @"0/300";
    [self.contentView addSubview:_textCountLabel];
    [_textCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(_textView).offset(0);
    }];
    

    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_textView.mas_bottom).offset(44);
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorWhite];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorBlack;
}

- (BOOL)shouldHideKeyboardWhenTouchInView:(UIView *)view {
    
    return YES;
}


- (void)updateWithVideoLinkContent:(NSString *)content
{
    self.textView.text = content;
}

#pragma mark - UINavigationControllerBackButtonHandlerProtocol

- (BOOL)shouldPopViewControllerByBackButtonOrPopGesture:(BOOL)byPopGesture {
    // 这里不要做一些费时的操作，否则可能会卡顿。
    if (([_squarePost.opus count] > 0) || ([_squarePost.videoFileUrl length] > 0) || (self.textView.text.length > 0)) {
        [HLUIHelper showAlertWithContent:@"是否放弃编辑的内容?" callBack:^(BOOL isConfirm) {
            if (isConfirm){
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
        return NO;
    } else {
        return YES;
    }
}


- (void)saveAction
{
    self.saveButton.enabled = NO;
    if ([_squarePost.dynamicType isEqualToString:@"2"] && ([_squarePost.opus count] == 0 && [_squarePost.dynamicOpusUrls count] == 0)){
        [QMUITips showWithText:@"配张图片更好哦~"];
        self.saveButton.enabled = YES;
        return;
    }
    
    if ([_squarePost.dynamicType isEqualToString:@"3"] && !([_squarePost.videoFileUrl length] != 0 || [_squarePost.videoLink length] != 0)){
        [QMUITips showWithText:@"配个视频更好哦~"];
        self.saveButton.enabled = YES;
        return;
    }
    
    
    if (_squarePost.opus.count > 0){
        MJWeakSelf
        [_squareImageOSSManager uploadDatas:_squarePost.opus callBack:^(NSArray<NSString *> *urls) {
            weakSelf.squarePost.dynamicOpusUrls = urls;
            weakSelf.squarePost.opus = nil;
            [weakSelf saveAction];
        }];
    }  else {
        [_squarePost clearCache];
        if (!(_squarePost.dynamicOpusUrls.count > 0) || !(_squarePost.videoLink.length > 0) || !(_squarePost.videoFileUrl.length > 0)){
            _squarePost.dynamicType = @"1";
        }
        _squarePost.videoDesc = _textView.text;
        NSDictionary *params = [_squarePost mj_JSONObject];
        self.request = [[HLVideoOptionRequest alloc] initWithType:HLVideoOptionTypeSave params:params];
        self.request.delegate = self;
        [self.request start];
    }
}

- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    HLVideoOptionRequest *videoPostRequest = (HLVideoOptionRequest *)request;
    if (videoPostRequest.isSuccess){
        if (_squarePost.videoFileUrl.length > 0){
            self.saveButton.enabled = YES;
        } else {
            [QMUITips showWithText:@"视频已经发布成功，请等待审核完成。"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else {
        self.saveButton.enabled = YES;
    }
    
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    self.saveButton.enabled = YES;
    [super requestFailed:request];
}



#pragma QMUITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView
{
    self.textCountLabel.text = [NSString stringWithFormat:@"%ld/300", textView.text.length];
}

- (BOOL)textView:(QMUITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text originalValue:(BOOL)originalValue
{
    return YES;
}

@end

